package com.lemonPiggy.message.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.lemonPiggy.message.domain.MessageTemplate;
import com.lemonPiggy.message.entity.MsgParams;
import com.lemonPiggy.message.util.PushMsgUtil;
import com.ruoyi.common.core.domain.R;
import com.lemonPiggy.message.service.IMessageTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 消息模板Controller
 * 
 * @author ruoyi
 * @date 2021-08-17
 */
@RestController
@RequestMapping("/template")
public class MessageTemplateController extends BaseController
{
    @Autowired
    private IMessageTemplateService messageTemplateService;
    @Autowired
    private PushMsgUtil pushMsgUtil;

    /**
     * 查询消息模板列表
     */
    @PreAuthorize(hasPermi = "system:template:list")
    @GetMapping("/list")
    public TableDataInfo list(MessageTemplate messageTemplate)
    {
        startPage();
        List<MessageTemplate> list = messageTemplateService.selectMessageTemplateList(messageTemplate);
        return getDataTable(list);
    }

    /**
     * 导出消息模板列表
     */
    @PreAuthorize(hasPermi = "system:template:export")
    @Log(title = "消息模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MessageTemplate messageTemplate) throws IOException
    {
        List<MessageTemplate> list = messageTemplateService.selectMessageTemplateList(messageTemplate);
        ExcelUtil<MessageTemplate> util = new ExcelUtil<MessageTemplate>(MessageTemplate.class);
        util.exportExcel(response, list, "消息模板数据");
    }

    /**
     * 获取消息模板详细信息
     */
    @PreAuthorize(hasPermi = "system:template:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(messageTemplateService.selectMessageTemplateById(id));
    }

    /**
     * 新增消息模板
     */
    @PreAuthorize(hasPermi = "system:template:add")
    @Log(title = "消息模板", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MessageTemplate messageTemplate)
    {
        return toAjax(messageTemplateService.insertMessageTemplate(messageTemplate));
    }

    /**
     * 修改消息模板
     */
    @PreAuthorize(hasPermi = "system:template:edit")
    @Log(title = "消息模板", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MessageTemplate messageTemplate)
    {
        return toAjax(messageTemplateService.updateMessageTemplate(messageTemplate));
    }

    /**
     * 删除消息模板
     */
    @PreAuthorize(hasPermi = "system:template:remove")
    @Log(title = "消息模板", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(messageTemplateService.deleteMessageTemplateByIds(ids));
    }

    /**
     * 发送消息
     */
    @PostMapping(value = "/sendMsg")
    public R<String> sendMessage(@RequestBody MsgParams msgParams) {
//        Result<MessageTemplate> result = new Result<MessageTemplate>();
        Map<String, String> map = null;
        try {
            map = (Map<String, String>) JSON.parse(msgParams.getTestData());
        } catch (Exception e) {
            return R.fail("解析Json出错！");
        }
        boolean is_sendSuccess = pushMsgUtil.sendMessage(msgParams.getMsgType(), msgParams.getTemplateCode(), map, msgParams.getReceiver());
        if (is_sendSuccess) {
            return R.ok("发送消息任务添加成功！");
        } else {
            return R.fail("发送消息任务添加失败！");
        }
    }
}
